package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import easier.sectionheader.decoration.AbsSlideIndexer;
import easier.sectionheader.decoration.AbsSlideIndexer.Builder;
import easier.sectionheader.decoration.CircleIndexer;
import easier.sectionheader.decoration.IndexerFactory;
import easier.sectionheader.decoration.SectionDataIndexer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity97 extends AppCompatActivity {

    private RecyclerView mRecycler;
    private List< Data > mDataList;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity97.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main97 );
        initView();
    }

    private void initView() {

        mockData();

        mRecycler = (RecyclerView) findViewById( R.id.recycler );
        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        mRecycler.setAdapter( new MultiTypeAdapter( convert() ) );

        SectionDataIndexer< Data > sectionIndexer = new SectionDataIndexer<>( mDataList ) {
            @Override
            protected boolean sectionEquals( Data data, String section ) {

                return Objects.equals( data.firstLetter, section );
            }

            @Override
            protected String[] createSections( List< Data > source ) {

                List< String > result = new ArrayList<>();
                for ( Data data : source ) {
                    String firstCharacter = data.firstLetter;
                    if ( !result.contains( firstCharacter.toUpperCase() ) ) {
                        result.add( firstCharacter.toUpperCase() );
                    }
                }
                String[] temp = new String[result.size()];
                return result.toArray( temp );
            }
        };

        StringBuilder sb = new StringBuilder();
        String[] sections = sectionIndexer.getSections();
        for ( String section : sections ) {
            sb.append( section );
        }

        AbsSlideIndexer balloonIndexer = new CircleIndexer( new Builder( this, sb.toString() ),
            getResources().getColor( R.color.teal_200 ), getResources().getColor( R.color.white ) );

        //IndexerFactory.quickAttach( mRecycler, sectionIndexer, balloonIndexer, color, color1, 50 );

        //IndexerFactory.quickAttachBallIndexer( mRecycler, sectionIndexer, 50 );

        //IndexerFactory.quickAttachCircleIndexer( mRecycler, sectionIndexer, 50 );
        //IndexerFactory.quickAttachCircleIndexer( mRecycler, sectionIndexer, 50, color, color1 );

        IndexerFactory.quickAttachSquareIndexer( mRecycler, sectionIndexer, 50 );
    }

    private void mockData() {

        mDataList = new ArrayList<>();
        mDataList.add( new Data( "A", "What" ) );
        mDataList.add( new Data( "A", "What" ) );
        mDataList.add( new Data( "A", "What" ) );
        mDataList.add( new Data( "A", "What" ) );
        mDataList.add( new Data( "B", "How" ) );
        mDataList.add( new Data( "C", "Hello" ) );
        mDataList.add( new Data( "C", "Hello" ) );
        mDataList.add( new Data( "C", "Hello" ) );
        mDataList.add( new Data( "D", "June" ) );
        mDataList.add( new Data( "E", "August" ) );
        mDataList.add( new Data( "E", "August" ) );
        mDataList.add( new Data( "E", "August" ) );
        mDataList.add( new Data( "E", "August" ) );
        mDataList.add( new Data( "E", "August" ) );
        mDataList.add( new Data( "F", "Times" ) );
        mDataList.add( new Data( "G", "Spring" ) );
        mDataList.add( new Data( "H", "Yellow" ) );
        mDataList.add( new Data( "H", "Yellow" ) );
        mDataList.add( new Data( "H", "Yellow" ) );
        mDataList.add( new Data( "H", "Yellow" ) );
        mDataList.add( new Data( "I", "Change" ) );
        mDataList.add( new Data( "J", "Geek" ) );
        mDataList.add( new Data( "K", "Dialog" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "L", "Activity" ) );
        mDataList.add( new Data( "M", "Bugs" ) );
        mDataList.add( new Data( "N", "Mission" ) );
        mDataList.add( new Data( "O", "Don't Know" ) );
        mDataList.add( new Data( "P", "View" ) );
        mDataList.add( new Data( "Q", "Holder" ) );
        mDataList.add( new Data( "R", "Request" ) );
        mDataList.add( new Data( "S", "Sorry" ) );
        mDataList.add( new Data( "T", "Temp" ) );
        mDataList.add( new Data( "U", "Ugly" ) );
        mDataList.add( new Data( "V", "Vivian" ) );
        mDataList.add( new Data( "W", "When" ) );
        mDataList.add( new Data( "X", "X is me" ) );
        mDataList.add( new Data( "Y", "Yes" ) );
        mDataList.add( new Data( "Z", "Zyk" ) );

    }

    private List< TypeViewHolder > convert() {

        ArrayList< TypeViewHolder > list = new ArrayList<>();

        for ( int i = 0; i < mDataList.size(); i++ ) {
            list.add( build( mDataList.get( i ) ) );
        }

        return list;
    }

    private TypeViewHolder build( Data data ) {

        return new BaseViewHolder( R.layout.activity_main97_item ) {
            @Override
            protected void onBind( ViewHolder viewHolder, View root, int position, int type, @NonNull List< Object > payloads ) {

                super.onBind( viewHolder, root, position, type, payloads );
                ((TextView) root.findViewById( R.id.first )).setText( data.text );
                ((TextView) root.findViewById( R.id.second )).setText( data.firstLetter );
            }
        };
    }

    private static class Data {

        String firstLetter;
        String text;

        public Data( String firstLetter, String text ) {

            this.firstLetter = firstLetter;
            this.text = text;
        }
    }
}